package com.tjetc.service.impl;

import com.tjetc.util.JwtUtils;
import com.tjetc.vo.AdminLoginVo;
import com.tjetc.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tjetc.mapper.AdminMapper;
import com.tjetc.domain.Admin;
import com.tjetc.service.AdminService;
import org.springframework.util.DigestUtils;

import java.util.HashMap;

@Service
public class AdminServiceImpl implements AdminService{

    @Resource
    private AdminMapper adminMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Admin record) {
        return adminMapper.insert(record);
    }

    @Override
    public int insertSelective(Admin record) {
        return adminMapper.insertSelective(record);
    }

    @Override
    public Admin selectByPrimaryKey(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Admin record) {
        return adminMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Admin record) {
        return adminMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result login(Admin admin) {
        String username = admin.getUsername();
        String password = admin.getPassword();
        Admin oldAdmin = adminMapper.selectByUsername(username);
        if (oldAdmin == null){
            return Result.fail("该用户不存在");
        }
        if (!oldAdmin.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
            return Result.fail("账号和密码不匹配");
        }
        oldAdmin.setPassword("******");
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",oldAdmin.getId());
        String token = JwtUtils.createJwtToken(map);
        AdminLoginVo adminLoginVo = new AdminLoginVo();
        adminLoginVo.setAdmin(oldAdmin);
        adminLoginVo.setToken(token);
        return Result.ok(adminLoginVo);
    }

    @Override
    public Result getMyDetails(Integer id) {
        Admin admin = adminMapper.findById(id);
        return admin==null?Result.fail("获取个人信息失败"):Result.ok(admin);
    }

    @Override
    public Result updateAdminDetails(Admin admin,String Opassword) {
      Admin admin1=adminMapper.selectByPrimaryKey(admin.getId());
      if (admin1!=null){
          if (admin1.getPassword().equals(DigestUtils.md5DigestAsHex(Opassword.getBytes()))){
            int i =  adminMapper.updateByPrimaryKeySelective(admin);
            return i>0?Result.ok("修改成功"):Result.fail("修改失败");
          }else {
              return Result.fail("原密码错误");
          }
      }else {
          return Result.fail("数据传输错误，管理员不存在");
      }
    }

    @Override
    public PageInfo<Admin> selAll(String username, Integer curPage, Integer pageSize) {
        PageHelper.startPage(curPage,pageSize);
        return new PageInfo<>(adminMapper.selAll(username));
    }

    @Override
    public Integer delById(Integer id) {
        return adminMapper.delById(id);
    }

    @Override
    public Integer update(Admin admin) {
             return   adminMapper.updateByPrimaryKeySelective(admin);

    }

    @Override
    public Integer add(Admin admin) {
        return adminMapper.add(admin);
    }


}
